package com.xzavier.yunlian_interview.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzavier.yunlian_interview.entity.Project;
import com.xzavier.yunlian_interview.entity.Tag;
import com.xzavier.yunlian_interview.mapper.ProjectMapper;
import com.xzavier.yunlian_interview.mapper.TagMapper;
import com.xzavier.yunlian_interview.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<Project> getProjects() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        List<Project> projects = projectMapper.selectList(null);
        for (Project project : projects) {
            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("project_id", project.getId());
            project.setTags(tagMapper.selectList(tagQueryWrapper).toArray(Tag[]::new));
        }
        return projects;
    }
}
